package business;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import dao.ClienteDAO;
import dao.DependenteDAO;
import db.DBUtil;
import bean.Cliente;
import bean.Dependente;

public class ClienteBusiness 
{
	private static ClienteBusiness instance = new ClienteBusiness();
	public static ClienteBusiness getInstance()
	{ return instance; }
	
	//private SessionFactory sessionFactory;
	
	public Integer salvar(Cliente cli)
	{
		Session session = DBUtil.getCurrentSession();
		Transaction tran = session.beginTransaction();
		
		try
		{
			Integer idCliente = ClienteDAO.getInstance().salvar(session, cli);
			cli.setId(idCliente);
			
			for(Dependente d : cli.getDependentes())
			{
				d.setCliente(cli);
			}
			DependenteDAO.getInstance().salvarDependente(session, cli.getDependentes());
			
			tran.commit();
			return 1;
		}
		catch(HibernateException ex)
		{
			tran.rollback();
			System.out.println("Erro ao salvar "+ex.getMessage());
			return 0;	
		}
	}
	
	public List<Cliente> buscarPorNome(String nome)
	{
		Session session = DBUtil.getCurrentSession();
		Transaction tran = session.beginTransaction();
		
		try
		{
			List<Cliente> listCliente = new ArrayList<Cliente>();
			
			listCliente = ClienteDAO.getInstance().buscarPorNome(session, nome);

			tran.commit();
			
			return listCliente;
			
		}
		catch(HibernateException ex)
		{
			tran.rollback();
			return new ArrayList<Cliente>();
		}
	}
	
	public Cliente buscarPorId(Integer idCliente)
	{
		Session session = DBUtil.getCurrentSession();
		Transaction tran = session.beginTransaction();
		
		try
		{
			Cliente cli = new Cliente();
			
			cli = ClienteDAO.getInstance().buscarPorId(session, idCliente);
			
			tran.commit();
			return cli;

		}
		catch(HibernateException ex)
		{
			tran.rollback();
			return new Cliente();
		}
	}

	public List<Cliente> buscarPorInfracoes(Integer min, Integer max)
	{
		Session session = DBUtil.getCurrentSession();
		Transaction tran = session.beginTransaction();
		
		try
		{
			List<Cliente> listCliente = new ArrayList<Cliente>();
			
			listCliente = ClienteDAO.getInstance().buscarPorQtdeInfracoes(session, min, max);
			
			tran.commit();
			
			return listCliente;

		}
		catch(HibernateException ex)
		{
			tran.rollback();
			return new ArrayList<Cliente>();
		}
	}

	public int atualizar(Cliente cliente)
	{
		Session session = DBUtil.getCurrentSession();
		Transaction tran = session.beginTransaction();
		
		try
		{
			ClienteDAO.getInstance().atualizar(session, cliente);
			
			tran.commit();
			return 1;
		}
		catch(HibernateException ex)
		{
			tran.rollback();
			return 0;
		}
	}

	public int excluir(Cliente cliente)
	{
		Session session = DBUtil.getCurrentSession();
		Transaction tran = session.beginTransaction();
		
		try
		{
			ClienteDAO.getInstance().excluir(session, cliente);
			
			tran.commit();
			return 1;
		}
		catch(HibernateException ex)
		{
			tran.rollback();
			return 0;
		}
	}
	
}
